<?php
namespace Amoysea\Controller;
use Think\Controller;
class InboundPlanController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
        $this->product = M('InboundPlan',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
    }
	//绑定发票
	//绑定
	public function bind_list_bill(){
		$m=M('BindListBill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$b=M('Bill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$l=M('List', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$ids=I('ids');
		$list_id=I('id');
		$data['list_id']=$list_id;
		foreach($ids as $k=>$v){
			$data['bill_id']=$v;
			$info=$m->where(array('bill_id'=>$v,'list_id'=>$list_id))->find();
			if($info){
				continue;
			}else{
				$res=$m->add($data);
				if(!$res){
					$return['status']=0;
					$return['message']="添加失败";
					$this->ajaxReturn($return);
				}else{
					$b->where(array('id'=>$v))->setField('bind_status',1);
					continue;
				}
			}
		}
		$l->where(array('id'=>$list_id))->setField('bind_status',1);
		$return['status']=1;
		$return['message']="添加成功";
		$this->ajaxReturn($return);
	}
	//绑定发票
	public function bind_bill(){
		$b=M('Bill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$m=M('BindListBill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$id=I('id');
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['no'] = array('like','%'.$keywords.'%');
			}
        }

		$map['bind_status']=0;
		$count=$b->where($map)->count();
		$list=$b->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		// dump($list);
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$bind_bill_list=$m->alias('m')->field('l.*')->join('__BILL__ l on m.bill_id=l.id')->where(array('list_id'=>$id))->select();
		// dump($bind_bill_list);
		// die;
        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign("bind_bill_list",$bind_bill_list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();
	}
	public function cost(){
		$Cost=M('Cost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Cost->where($map)->count();
		$list=$Cost->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	public function add_cost(){
		$m=M('ListCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$list_id=I('list_id');
		$ids=I('ids');
		$data['list_id']=$list_id;
		$count=0;
		$count_fail=0;
		foreach($ids as $k=>$v){
			$data['cost_id']=$v;
			$info=$m->where(array('list_id'=>$list_id,'cost_id'=>$v))->find();
			if(!$info){
				$res=$m->add($data);
				if(!$res){
					$return['status']=0;
					$return['message']="添加失败";
					$this->ajaxReturn($return);
				}else{
					$count++;
				}
			}else{
					$count_fail++;
			}
		}
		$return['status']=1;
		$return['message']=$count."个添加成功,".$count_fail."个添加失败";
		$this->ajaxReturn($return);
	}
	//修改
	public function cost_edit(){
		$m=M('ListCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$prices=I('prices',trim);
		$ids=I('ids',trim);

		for($i=0;$i<count($prices);$i++){
			$data['total_fee']=$prices[$i];
			$id=$ids[$i];
			$res=$m->where(array('id'=>$id))->save($data);

			if($res!==false){
				
				continue;
			}else{
				$return['status']=0;
				$return['message']="更新失败！";
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']="更新成功！";
		$this->ajaxReturn($return);
	}
	//采购单修改
	public function list_edit(){
		$List=M('List', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['order_no'] = array('like','%'.$keywords.'%');
			}
        }
		$map['is_examined']=4;
		$count=$List->where($map)->count();
		$list=$List->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$times=time()-strtotime($v['created_at']);
			$list[$k]['arrival_bill_days']=sprintf('%.2f',$times/3600);//到票时间
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//价格修改
	public function price_edit(){
		$m=M('ListProduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$prices=I('prices',trim);
		$ids=I('ids',trim);
		for($i=0;$i<count($prices);$i++){
			$data['price']=$prices[$i];
			$id=$ids[$i];
			$res=$m->where(array('id'=>$id))->save($data);
			if($res!==false){
				$this->massage('采购单','价格修改','产品信息(ID='.$id.')价格修改为'.$prices[$i]);
				if($res==1){//推送数据
					$ss=$this->push_price($id,$prices[$i]);
					// dump($ss);
					// die;
					if($ss){
						continue;
					}else{
						$return['status']=0;
						$return['message']="推送价格信息失败！";
						$this->ajaxReturn($return);
					}
				}
				
			}else{
				$return['status']=0;
				$return['message']="更新失败！";
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']="更新成功！";
		$this->ajaxReturn($return);
	}
	//价格分摊
	public function price_share(){
		$m=M('ListCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$id=I('id');
        session('order_no',null);
        session('s_list_product_id',null);
        $g=session('company');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');
        $this->p = M('list_product',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list=$this->p->field($f."product.name,".$f."product.unit_no as u_n,".$f."product.unit_no_t as u_n_t,".$f."product.unit as u_name,".$f."product.unit_t as u_name_t,".$f."product.category,".$f."product.hs_code,".$f."product.goods_no,".$f."list_product.*")
            ->join($f."product on ".$f."list_product.product_id=".$f."product.id",'left')
            ->order($f."product.name")
            ->where($f."list_product.list_id='".$id."'")->select();
		$list_cost=$m->alias('m')->field('m.*,c.no,c.name')->join('__COST__ c on m.cost_id=c.id')->where(array('list_id'=>$id))->select();
        $examined=$this->product->where(array('id'=>$id))->getField('is_examined');
        $this->assign('is_examined',$examined);
        $this->assign('order_no',$id);
        $this->assign('list',$list);
		$this->assign('list_cost',$list_cost);
        $this->display();
	}
	//按数量分摊
	public function share_num(){
		$id=I('id');
		$m=M('ListCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('ListProduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$cost_list=$m->where(array('list_id'=>$id))->select();
		$total_fee=0;
		foreach($cost_list as $k=>$v){
			$total_fee+=$v['total_fee'];
		}
		$product_list=$n->where(array('list_id'=>$id))->select();
		$total_nums=0;
		foreach($product_list as $ks=>$vs){
			$total_nums+=$vs['count'];
		}
		foreach($product_list as $kss=>$vss){
			$data['share_fee']=$vss['count']/$total_nums*$total_fee;
			$res=$n->where(array('id'=>$vss['id']))->save($data);
			// echo $n->getlastsql();
			// die;
			if($res!==false){
				continue;
			}else{
				$return['status']=0;
				$return['message']="更新失败！";
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']="更新成功！";
		$this->ajaxReturn($return);
	}
	//按金额分摊
	public function share_fee(){
		$id=I('id');
		$m=M('ListCost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$n=M('ListProduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$cost_list=$m->where(array('list_id'=>$id))->select();
		$total_fee=0;
		foreach($cost_list as $k=>$v){
			$total_fee+=$v['total_fee'];
		}
		$product_list=$n->where(array('list_id'=>$id))->select();
		$total_price=0;
		foreach($product_list as $ks=>$vs){
			$total_price+=$vs['count']*$vs['price'];
		}
		foreach($product_list as $kss=>$vss){
			$data['share_fee']=$vss['count']*$vss['price']/$total_price*$total_fee;
			$res=$n->where(array('id'=>$vss['id']))->save($data);
			// echo $n->getlastsql();
			// die;
			if($res!==false){
				continue;
			}else{
				$return['status']=0;
				$return['message']="更新失败！";
				$this->ajaxReturn($return);
			}
		}
		$return['status']=1;
		$return['message']="更新成功！";
		$this->ajaxReturn($return);
	}
	//推送价格
	public function push_price($id,$price){
        if($id==''){
            alert(0,'操作有误');
        }
        $g=session('company');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');////因为对方现在有list_product表的id字段---gooods_id  所以不需要那么复杂
        $this->p = M('list_product',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->p->where("id='".$id."'")->find();
        $data=array(
            'erp_code'=>$g['company_id'],
            'goods_id'=>$result['id'],
            'price'=>$price,
        );
        ////根据参数找到对应的仓库
        $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id",'left')
            ->where(C('DB_PREFIX')."warehouse.warehouse_code='".$result['warehouse_code']."'")
            ->getField('vars,url,warehouse_code');
        $url=$d['var_7']['url'];
		
        $warehouse_code=$d['var_3']['warehouse_code'];
        $data['warehouse_code']=$warehouse_code;
        $return =push($url,$data);
        $result=json_decode($return,true);
        if($result['status']==1){
            return 1;
        }else{
            return 0;
        }
    }
	//采购单详情
    public function list_edit_detail(){
		$id=I('id');
        session('order_no',null);
        session('s_list_product_id',null);
        $g=session('company');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');
        $this->p = M('list_product',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list=$this->p->field($f."product.name,".$f."product.unit_no as u_n,".$f."product.unit_no_t as u_n_t,".$f."product.unit as u_name,".$f."product.unit_t as u_name_t,".$f."product.category,".$f."product.hs_code,".$f."product.goods_no,".$f."list_product.*")
            ->join($f."product on ".$f."list_product.product_id=".$f."product.id",'left')
            ->order($f."product.name")
            ->where($f."list_product.list_id='".$id."'")->select();
        $examined=$this->product->where(array('id'=>$id))->getField('is_examined');
        $this->assign('is_examined',$examined);
        $this->assign('order_no',$id);
        $this->assign('list',$list);
        $this->display();
    }
	public function print_examinedetail(){
			$param = I('param.','','trim');
			$id=$param['id'];
			
			$list_pro_model = M('ListProduct', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$country_model = M('country', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$list_model=M('List', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$coin_model = M('coin', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$unit = M('Unit', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$Product = M('Product', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			
			$warehouse=M('Warehouse');
			
			$country = $country_model->select();
			
			$detail=$list_model->where('id='.$id)->find();
			
			$detail['section_name']=$Section->where(array('no'=>$detail['section_no']))->find();
			$detail['company_name']=$ContactsCompany->where(array('no'=>$detail['contacts_company_no']))->find();//往来单位
			$detail['people_make_name']=$Personnel->where(array('no'=>$detail['people_make_no']))->find();//制单人
			$detail['people_purchasing_name']=$Personnel->where(array('no'=>$detail['people_purchasing_no']))->find();
			
			
			$listrows=10;

			$p=I('p')?I('p'):1;
			$firstrow=($p-1)*$listrows;

			$map['list_id']=$id;
			$count=$list_pro_model->where($map)->count();
			$list   = $list_pro_model->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
			// dump($list);
			// die;
			$Page    = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show    = $Page->show();// 分页显示输出

			$count_count=0;//主数量
			$count_price=0;//销售总金额
			$count_second_count=0;//辅数量
			foreach($list as $k=>$v){
				$list[$k]['warehouse_info']=$warehouse->where(array('warehouse_code'=>$v['warehouse_code']))->find();
				$list[$k]['product']=$Product->where(array('id'=>$v['product_id']))->find();
				$count_count+=$v['count'];
				$count_price+=$v['count']*$v['price'];
				$count_second_count+=$v['unit_t_count'];
            }
			
			$this->assign('count_count',$count_count);
			$this->assign('count_price',$count_price);
			$this->assign('count_second_count',$count_second_count);
			
			
			$this->assign('page',$show);// 赋值分页输出
			$this->assign("list",$list);
			$this->assign("country",$country);
            $this->assign('detail',$detail);
			$this->assign('data',$data);
		$this->display();
	}
    public function index(){
        if(!isset($_GET['p']) || $_GET['p']==''){
            $p=1;
        }else{
            $p=intval($_GET['p']);
        }
        if(I("get.start_time")!='') {
            $where['created_at']= array('gt',I("get.start_time"));
        }
        if(I("get.end_time")!=''){
            $where['created_at'] = array('lt',I("get.end_time"));
        }
        if(I("get.start_time")!='' && I("get.end_time")!=''){
            $where['created_at']= array(array('gt',I("get.start_time")),array('lt',I("get.end_time")));
        }
        if(I("get.name")!=''){
            $where['order_no']=array('like',"%".I("get.name")."%");
        }
        if(I("get.page_count")=='' || I("get.page_count")<=0){
            $page_count=15;
        }else{
            $page_count=intval(I("get.page_count"));
        }
        if(I("get.sort")!=''){
            $order=I("get.sort")." ".I("get.dec");
        }else{
            $order='id desc';
        }
        if(!isset($where)){
            $where="1=1";
        }
        $list = $this->product->where($where)->order($order)->page($p.','.$page_count)->select();
        $count      = $this->product->where($where)->count();
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,$page_count);
        $show       = $Page->show();
        //////////
        $g=session('company');
        $db_name=$g['db'];
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        foreach($list as $k=>$v){
            $list[$k]['some_back']=$this->p->where("list_id='".$v['id']."' and is_examined=1")->count();
            $list[$k]['all_back']=$this->p->where("list_id='".$v['id']."'")->count();
        }
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->display();
    }
    public function add(){//注意
        if(session('order_no')){
            $g=session('company');
            $db_name=$g['db'];
            $this->p = M('product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$this->book = M('Book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$book_list=$this->book->where(array('type'=>2,'examined'=>2))->select();
			$this->assign('book_list',$book_list);
			
            $id=session('s_list_product_id');//id=1,2,34,的形式
            $where['id']=array('in',$id);
            $list=$this->p->where($where)->select();
            $count=$this->product->where(array('order_no'=>session('order_no')))->count();
            $this->assign('is_add_list',$count);
            if($count>0){
                $this->pp = M('list',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
                $result=$this->pp->where(array('order_no'=>session('order_no')))->find();
                $this->assign('home',$result);
            }
			foreach($list as $ks=>$vs){
				$product_info=M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)->where(array('product_id'=>$vs['id'],'list_id'=>$result['id']))->find();
				
				if($product_info['warehouse_code']){
					$list[$ks]['warehouse_code']=$product_info['warehouse_code'];
				}else{
					$list[$ks]['warehouse_code']="";
				}
			}
            $this->assign('list',$list);
            //////////获取清单内容
            $list_id=$this->product->where(array('order_no'=>session('order_no')))->getField('id');
            $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
            $result=$this->p->field(C('DB_PREFIX_T')."product.*,".C('DB_PREFIX_T')."list_product.*")
                ->join('__PRODUCT__ on __LIST_PRODUCT__.product_id=__PRODUCT__.id','left')
                ->where(array(C('DB_PREFIX_T').'list_product.list_id'=>$list_id))
                ->order(C('DB_PREFIX_T')."product.name,".C('DB_PREFIX_T')."list_product.created_at")
                ->select();
            if($result){
                $this->assign('result',$result);
            }
            ////获取仓库的数量
            $warehouse_id = M('user_warehouse')->where("user_id='".$g['company_id']."'")->getField('warehouse_group_id');
            $w_count=substr_count($warehouse_id,',')+1;
            $this->assign('w_count',$w_count);
        }else{
            $g=session('company');
            $id=$this->product->order("id desc")->getField('id');
            session('order_no',date("Ymd",time()).$g['company_id'].$id.rand(1000000,9999999));
        }
        $this->display();
    }
    public function detail_add($id){
        if($id==''){
            echo '操作有误';
            exit;
        }
        $order_no=$this->product->where(array('id'=>$id))->getField('order_no');
        if($order_no==''){
            echo '操作有误';
            exit;
        }
        session('order_no',$order_no);
        $g=session('company');
        $db_name=$g['db'];
        $result=$this->product->where(array('order_no'=>session('order_no')))->find();
        $this->assign('home',$result);
        $product_id=M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)->distinct('product_id')->where(array('list_id'=>$id))->getField('product_id',true);
        $this->p = M('product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        if(session('s_list_product_id')){
            if($product_id===null){
                $product_id=array();
            }
            $session=explode(',',session('s_list_product_id'));
            foreach($session as $v){
                array_push($product_id,$v);
            }
            $where['id']=array('in',$product_id);
        }else{
            $where['id']=array('in',$product_id);
            session('s_list_product_id',implode(',',$product_id));
        }

        $list=$this->p->where($where)->select();
		foreach($list as $ks=>$vs){
			$product_info=M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)->where(array('product_id'=>$vs['id'],'list_id'=>$id))->find();
			
			if($product_info['warehouse_code']){
				$list[$ks]['warehouse_code']=$product_info['warehouse_code'];
			}else{
				$list[$ks]['warehouse_code']="";
			}
		}
		// dump($list);
		// die;
        $this->assign('list',$list);
        //////////获取清单内容
        $list_id=$this->product->where(array('order_no'=>session('order_no')))->getField('id');
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->p->field(C('DB_PREFIX_T')."product.*,".C('DB_PREFIX_T')."list_product.*")
            ->join('__PRODUCT__ on __LIST_PRODUCT__.product_id=__PRODUCT__.id','left')
            ->order(C('DB_PREFIX_T')."product.name,".C('DB_PREFIX_T')."list_product.created_at")
            ->where(array(C('DB_PREFIX_T').'list_product.list_id'=>$list_id))->select();
        if($result){
            $this->assign('result',$result);
        }
        ////获取仓库的数量
        $warehouse_id = M('user_warehouse')->where("user_id='".$g['company_id']."'")->getField('warehouse_group_id');
        $w_count=substr_count($warehouse_id,',')+1;
        $this->assign('w_count',$w_count);
        $this->display();
    }
    public function get_list_product($product_id){
        $g=session('company');
        $db_name=$g['db'];
        //////////获取清单内容
        $list_id=$this->product->where(array('order_no'=>session('order_no')))->getField('id');
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->p->where(array('list_id'=>$list_id,'product_id'=>$product_id))->select();
        if($result){
            alert(1,$result);
        }else{
            alert(0,'无');
        }
    }
    ////////采购单基本信息的添加
    public function addD($purchaser,$making,$department,$app_reasons,$from_company,$manua_id){
        if(session('order_no')==''){
            alert(0,'采购单号不能为空');
        }
        if($making==''){
            alert(0,'制单人不能为空');
        }
        if($department==''){
            alert(0,'部门不能为空');
        }
        $data['order_no']=session('order_no');
        if($this->product->where($data)->count()>0){
            alert(0,'账册编码或者采购单号已经存在');
        }

        $data['department']=$department;
        $data['making']=$making;
        $data['purchaser']=$purchaser;
        $data['app_reasons']=$app_reasons;
        $data['from_company']=$from_company;
		$data['manua_id']=$manua_id;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$this->product->add($data);
        if($result){
            alert($result,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    //采购单基本信息编辑界面
    public function edit($id){
        if($id==''){
            echo "错误";
        }
		$g=session('company');
        $db_name=$g['db'];
		$this->book = M('Book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		$book_list=$this->book->where(array('type'=>2,'examined'=>2))->select();
		
		$this->assign('book_list',$book_list);
        $this->result=$this->product->find($id);
        $this->display();
    }
    //采购单编辑
    public function editD($id,$purchaser,$making,$department,$app_reasons,$from_company,$manua_id){
        if($id==''){
            alert(0,'操作有误');
        }
        if($purchaser=='' || $making==''){
            alert(0,'制单人和采购人不能为空');
        }
        $data['purchaser']=$purchaser;
        $data['making']=$making;
        $data['department']=$department;
        $data['app_reasons']=$app_reasons;
        $data['from_company']=$from_company;
		$data['manua_id']=$manua_id;
        $massage=session('company');
        $data['operator']=$massage['username'];
        $result=$this->product->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    //采购单删除
    public function del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $where['id']=array('in',$group_id);
        $result=$this->product->where("is_examined <> 4")->where($where)->delete();
        if($result){
            //////删除详情
            $g=session('company');
            $db_name=$g['db'];
            $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
            $n_where['list_id']=array('in',$group_id);
            $this->p->where($n_where)->delete();
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else {
            alert(0, '该采购单已经推送，不能删除');
        }
    }
    //产品选择界面
    public function product_add(){
        if(isset($_GET['id']) && $_GET['id']!==''){
            session('order_no',$_GET['id']);
        }
        $g=session('company');
        $db_name=$g['db'];

        if(session('order_no')===null){
            echo "操作有误";exit;
        }

		$page_count=5;
        if(!isset($_GET['p']) || $_GET['p']==''){
            $p=1;
        }else{
            $p=intval($_GET['p']);
        }
		$manua_id=I('manua_id');
        $url=C('base_config.manual_list');
        $data=array(
             'manua_id'=>$manua_id,
             'p'=>$p,
         );
		
        $true=send_curl($url,$data);//推送过去
        $true=json_decode($true,'true');

		if($true['flag']){
			$count      = $true['data']['count'];
			$this->assign('count',$count);
			$Page       = new \Think\Page($count,$page_count);
			$show       = $Page->show();
			$this->assign('page',$show);
			$this->assign('list',$true['data']['info']);
			$this->display();
		}else{
			$this->error('没有数据');
		}
        
    }
    //产品添加界面  添加到采购单  下一步就是分仓库
    public function product_addD($id){
        $order_no=session('order_no');
        if(session('order_no')===null || session('order_no')==''){
            alert(0,'该步操作已经失效，请到采购单界面选择该采购单重新添加产品');
        }
        $group_id=explode(',',$id);
        if(count($group_id)==0){
            alert(0,'操作有误');
        }
        $url=C('base_config.manual_info');

        foreach($group_id as $id){
			$data=array(
				 'id'=>$id
			);
			$true=send_curl($url,$data);//推送过去
			$true=json_decode($true,'true');
			// dump($url);
			// dump($data);
			// dump($true);
			// die;
            if($true['flag']==0){
                alert(0,'存在某个产品不存在，请重新打开产品选择界面');
            }
        }
        $id=implode(',',$group_id);
        if(session('s_list_product_id')){//非第一次添加
            session('s_list_product_id',session('s_list_product_id').','.$id);
        }else{
            session('s_list_product_id',$id);//将添加的产品id放在session中  //第一次添加
        }
        alert(1,'添加成功');
    }
    public function product_del($id){
        $g=session('company');
        $db_name=$g['db'];
        $group_id=explode(',',session('s_list_product_id'));
        if(in_array($id,$group_id)){
            $this->product = M('list',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
            $list_id=$this->product->where(array('order_no'=>session('order_no')))->getField('id');
            foreach($group_id as $k=>$v){
                if($v==$id){
                    unset($group_id[$k]);
                    $group_id=array_values($group_id);
                    $ids=implode(',',$group_id);
                    $_SESSION['s_list_product_id']=$ids;
                }
            }
            $this->product = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
            if($this->product->where(array('list_id'=>$list_id,'product_id'=>$id))->count()>0){
                if($this->product->where(array('list_id'=>$list_id,'product_id'=>$id))->delete()){
                    alert(1,'删除成功');
                }else{
                    alert(0,'删除失败');
                }
            }else{
                if(in_array($id,$group_id)){
                    alert(0,'删除失败');
                }else{
                    alert(1,'删除成功');
                }
            }
        }
    }
    //删除全过程的session
    public function warehouse_select_session_delete(){
        session('order_no',null);
        session('s_list_product_id',null);
        alert(1,'成功');
    }
    //仓库选择界面，根据当前的产品来选择仓库
    public function warehouse_select($id){
        $g=session('company');
        $db_name=$g['db'];
        $this->product = M('product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $product=$this->product->where("id='".$id."'")->find();
        $warehouse_id = M('user_warehouse')->where("user_id='".$g['company_id']."'")->getField('warehouse_group_id');
        $group_id=explode(',',$warehouse_id);
        $where['id']=array('IN',$group_id);
        $warehouse = M('warehouse')->where($where)->order("id desc")->getField('warehouse_code,name',true);
        $this->assign('warehouse',$warehouse);
        $this->assign('id',$id);
        $this->assign('product',$product);
        $this->display();
    }

    //最终将采购单  产品 以及对应的各个仓库统一推送到数据库
    public function all_product_addD($product_id,$count,$unit,$unit_no,$price,$unit_t,$unit_t_no,$unit_t_count,$other_count,$warehouse_name,$warehouse_code,$expected_time){
		// dump(I());
		// die;
        $g=session('company');
        $db_name=$g['db'];
        if(session('order_no')===null){
            alert(0,'操作有误');
        }
        if($product_id=='' || $count=='' || $unit=='' || $unit_no=='' || $price=='' || $warehouse_name=='' || $warehouse_code=='' || $expected_time==''){
            alert(0,'请填写完整');
        }
        $count=explode(',',$count);
        foreach($count as $v){
            if(!preg_match('/^[1-9]+[0-9]+$/',$v)){
                alert(0,'数量必须为整数');
            }
        }
        $unit_name=explode(',',$unit);
        $unit_no=explode(',',$unit_no);
		$unit_t_name=explode(',',$unit_t);
		$unit_t_no=explode(',',$unit_t_no);
		$unit_t_count=explode(',',$unit_t_count);
		$other_count=explode(',',$other_count);
        $price=explode(',',$price);
        $expected_time=explode(',',$expected_time);
        $warehouse_name=explode(',',$warehouse_name);
        $warehouse_code=explode(',',$warehouse_code);
        for($i=0;$i<count($expected_time);$i++){
            if($expected_time[$i]<date("Y-m-d",time())){
                alert(0,'时间格式不正确');
            }
        }
        $product= M('product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        if($product->where(array('id'=>$product_id))->getField('examined')!=2){
            alert(0,'操作有误');
        }
        $this->ps = M('list',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list_id=$this->ps->where(array('order_no'=>session('order_no')))->getField('id');
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        ///如果对应的产品 不存在  说明是新增  新增 如下
        if($this->p->where(array('list_id'=>$list_id,'product_id'=>$product_id))->count()==0){
            $dataList=array();
            foreach($warehouse_code as $k=>$v){
                $dataList[]=array(
                    'list_id'=>$list_id,
                    'product_id'=>$product_id,
                    'operator'=>$g['username'],
                    'count'=>$count[$k],
                    'unit_name'=>$unit_name[$k],
                    'unit_no'=>$unit_no[$k],
					'unit_t_name'=>$unit_t_name[$k],
					'unit_t_no'=>$unit_t_no[$k],
					'unit_t_count'=>$unit_t_count[$k],
					'other_count'=>$other_count[$k],
                    'price'=>$price[$k],
                    'warehouse_name'=>$warehouse_name[$k],
                    'warehouse_code'=>$warehouse_code[$k],
                    'expected_time'=>$expected_time[$k],
                );
            }
            $result=$this->p->addAll($dataList);
        }else{
            foreach($warehouse_code as $k=>$v){
                $dataList=array(
                    'list_id'=>$list_id,
                    'product_id'=>$product_id,
                    'operator'=>$g['username'],
                    'count'=>$count[$k],
                    'unit_name'=>$unit_name[$k],
                    'unit_no'=>$unit_no[$k],
					'unit_t_name'=>$unit_t_name[$k],
					'unit_t_no'=>$unit_t_no[$k],
					'unit_t_count'=>$unit_t_count[$k],
					'other_count'=>$other_count[$k],
                    'price'=>$price[$k],
                    'warehouse_name'=>$warehouse_name[$k],
                    'warehouse_code'=>$warehouse_code[$k],
                    'expected_time'=>$expected_time[$k],
                );
                if($this->p->where(array('list_id'=>$list_id,'product_id'=>$product_id,'warehouse_name'=>$dataList['warehouse_name']))->count()==0){
                    $result=$this->p->add($dataList);
                }else{
                    $result=$this->p->where(array('list_id'=>$list_id,'product_id'=>$product_id,'warehouse_name'=>$dataList['warehouse_name']))->save($dataList);
                }
            }
        }
		// dump($dataList);
		// die;
        $del['warehouse_code']=array('notin',$warehouse_code);
        $del_result=$this->p->where($del)->delete();
        if($result!==false){
            alert(1,'操作成功');
        }elseif($del_result){
            alert(1,'操作成功');
        }else{
            alert(0,'操作失败');
        }
    }
    //采购单详情
    public function detail($id){
        session('order_no',null);
        session('s_list_product_id',null);
        $g=session('company');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');
        $this->p = M('list_product',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list=$this->p->field($f."product.name,".$f."product.unit_no as u_n,".$f."product.unit_no_t as u_n_t,".$f."product.unit as u_name,".$f."product.unit_t as u_name_t,".$f."product.category,".$f."product.hs_code,".$f."product.goods_no,".$f."list_product.*")
            ->join($f."product on ".$f."list_product.product_id=".$f."product.id",'left')
            ->order($f."product.name")
            ->where($f."list_product.list_id='".$id."'")->select();
        $examined=$this->product->where(array('id'=>$id))->getField('is_examined');
        $this->assign('is_examined',$examined);
        $this->assign('order_no',$id);
        $this->assign('list',$list);
        $this->display();
    }
    //详情删除
    public function detail_del($id){
        $g=session('company');
        $db_name=$g['db'];
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=$this->p->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
    //详情的编辑
    public function product_editD($id,$type){
        $g=session('company');
        $db_name=$g['db'];
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        if($id=='' || $type==''){
           alert(0,'操作有误');
        }
        if($type=='count'){
            $data['count']=I("post.count");
        }elseif($type=='unit'){
            $data['unit_name']=I("post.unit_name");
            $data['unit_no']=I("post.unit_no");
        }elseif($type=='time'){
            $data['expected_time']=I("post.time");
        }else{
            alert(0,'操作有误');
        }
        $where['id']=$id;
        $data['operator']=$g['username'];
        $result=$this->p->where($where)->save($data);
        if($result){
            alert(1,'更改成功');
        }else{
            alert(0,'更改失败');
        }
    }
    /////////已经实现推向多仓   每次只能推一个采购单
    public function push($id){
        $g=session('company');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');
        $this->p = M('list',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list=$this->p->field($f."list.id,".$f."list.is_examined,".$f."list.order_no")->where("id='".$id."'")->find();
        $list['erp_code']=$g['company_id'];
        //////如果已经推送则不能再次推送
        if($list['is_examined']==4){
            alert(0,'请不要重复推送');
        }
        unset($list['is_examined']);
        $this->p = M('list_product',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $data=$this->p
            ->field($f."list_product.id as goods_id,".$f."list_product.unit_t_no,".$f."list_product.unit_t_count,".$f."list_product.other_count,".$f."product.pn,".$f."product.standard,".$f."product.unit_t_goods_spec,".$f."product.goods_no,".$f."product.name as goods_name,".$f."product.zccode as manual_id,".$f."list_product.count as goods_count,".$f."list_product.expected_time,".
                $f."list_product.unit_no,".$f."list_product.price as goods_price,".$f."list_product.warehouse_code as w_code,".$f."product.hs_code as hscode")
            ->join($f."product on ".$f."list_product.product_id=".$f."product.id",'left')
            ->where($f."list_product.list_id='".$id."' and ".$f."list_product.is_push=0")
            ->select();
        $ware=array();
        foreach($data as $v){
            $ware[$v['w_code']][]=$v;
        }
        $suc_count=0;
        $all_count=0;
        foreach($ware as $k=>$v){
           $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id")->where(C('DB_PREFIX')."warehouse.warehouse_code='".$k."'")->getField('vars,url,warehouse_code');
           $url=$d['var_1']['url'];
           $warehouse_code=$d['var_1']['warehouse_code'];
           $list['warehouse_code']=$warehouse_code;
            $data=array(
                'list'=>$list,
                'goods'=>$v,
            );
           $true=push($url,array(0=>$data));//推送过去
           $true=json_decode($true,'true');
           if($true['status']==1){
               $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
               $result=$this->p->where("list_id='".$id."' and warehouse_code='".$k."'")->setField('is_push',1);
               if($result){
                   $suc_count++;
               }
           }
            $all_count++;
       }
        if($suc_count==$all_count){
            if($this->product->where("id='".$id."'")->setField('is_examined',4)){
                alert(1,'推送成功');
            }else{
                alert(0,'推送成功，采购单状态更新失败');
            }
        }else{
            alert(0,$suc_count.'条推送成功，'.($all_count-$suc_count).'条推送失败');
        }
    }
    function getSelected(){
        $g=session('company');
        $db_name=$g['db'];
        $list_id=$this->product->where(array('order_no'=>session('order_no')))->getField('id');
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $product_id=$this->p->where(array('list_id'=>$list_id))->distinct('product_id')->getField('product_id',true);
        if(count($product_id)==0){
            alert(0,'暂无');
        }else{
            alert(1,$product_id);
        }
    }
    ////////三级审批//创建多个函数。方便权限的创建于管理
    public function examined_1($id){
        $is_examined=$this->product->where("id='".$id."'")->getField('is_examined');
        if($is_examined!=0){
            alert(0,'操作有误');
        }
        if($this->product->where("id='".$id."'")->setField('is_examined',1)){
            alert(1,'操作成功');
        }else{
            alert(0,'操作有误');
        }
    }
    public function examined_2($id){
        $is_examined=$this->product->where("id='".$id."'")->getField('is_examined');
        if($is_examined!=1){
            alert(0,'操作有误');
        }
        if($this->product->where("id='".$id."'")->setField('is_examined',2)){
            alert(1,'操作成功');
        }else{
            alert(0,'操作有误');
        }
    }
    public function examined_3($id){
        $is_examined=$this->product->where("id='".$id."'")->getField('is_examined');
        if($is_examined!=2){
            alert(0,'操作有误');
        }
        if($this->product->where("id='".$id."'")->setField('is_examined',3)){
            alert(1,'操作成功');
        }else{
            alert(0,'操作有误');
        }
    }
	
	public function massage($type,$title,$detail){
        $massage['url']='';
        $massage['type']=$type;
        $massage['title']=$title;
        $massage['detail']=$detail;
		M('massage', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->add($massage);
    }
}